package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author guanghaocheng
 * @version 1.0
 * 翼以尘雾之微补益山海,荧烛末光增辉日月
 * @date 2020/12/25 21:36
 */
//自定义ListValue注解的校验器
public class ListValueConstraintValid implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<Integer>();
    //初始化方法
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if(vals.length>0) {
            for (int val : vals) {
                set.add(val);
            }
        }
    }

    //判断是否校验成功
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {

        return set.contains(value);
    }


}
